package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.*;
import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.ShippingService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

@Service
public class ShippingServiceImpl implements ShippingService {
    @Autowired
    private ShippingDao shippingDao;

    @Autowired
    private ExportDao exportDao;

    @Autowired
    private PackingDao packingDao;



    @Override
    public void save(Shipping shipping) {
        String id = shipping.getShippingOrderId();
        Packing packing = packingDao.selectByPrimaryKey(id);
        if (!packing.getState().equals("0")){
            return;
        }else {
            packing.setState("1");
            packingDao.updateByPrimaryKeySelective(packing);
            shipping.setState(0);
            shippingDao.insertSelective(shipping);
        }
        shippingDao.insertSelective(shipping);

    }



    @Override
    public PageInfo<Shipping> findByPage(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        ShippingExample shippingExample = new ShippingExample();
        shippingExample.createCriteria();
        List<Shipping> list = shippingDao.selectByExample(shippingExample);
        return new PageInfo<>(list);
    }

    @Override
    public String delete(String id) {
        Shipping shipping = shippingDao.selectByPrimaryKey(id);
        Integer state = shipping.getState();
        if (state!=null){
            if (state==0){
                shippingDao.deleteByPrimaryKey(id);
                Packing packing = packingDao.selectByPrimaryKey(id);
                if (packing!=null){
                    packing.setState("0");
                    packingDao.updateByPrimaryKeySelective(packing);
                }
                return "1";

            }
        }
        return "0";
    }

    @Override
    public Shipping findById(String id) {
        return shippingDao.selectByPrimaryKey(id);
    }

    @Override
    public Shipping findByPackingId(String packingId) {
        Shipping shipping = new Shipping();
        shipping.setShippingOrderId(packingId);
        //信用证号
        String lcno="";
        String destinationPort="";
        String shipmentPort="";
        Packing packing = packingDao.selectByPrimaryKey(packingId);
        if (packing!=null){
            String exportIds = packing.getExportIds();
            if (exportIds!=null){
                String[] split = exportIds.split(",");
                List<String> exportIdList = Arrays.asList(split);
                if (exportIdList!=null&&exportIdList.size()>0){
                    String s = exportIdList.get(0);
                    Export export = exportDao.selectByPrimaryKey(s);
                    lcno=export.getLcno();
                    destinationPort=export.getDestinationPort();
                    shipmentPort=export.getShipmentPort();
                }
            }
        }
        shipping.setPortOfDischarge(destinationPort);
        shipping.setPortOfLoading(shipmentPort);
        shipping.setLcNo(lcno);
        return shipping;
    }


}
